#!/usr/bin/python
#
# Project line count
#
#  Copyright 2006 Corey Tabaka
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

import sys
import os
import re

if __name__=="__main__":
	if len(sys.argv) == 2:
		cwd = sys.argv[1]
	else:
		cwd = os.getcwd()
	
	dircount = 0
	filecount = 0
	linecount = 0
	extensions = ( '.h', '.c', '.cc', '.cpp', '.asm', '.py', '.sed', '.ld' )
	pattern = re.compile('\.lzss|\.o|\.tga')
	
	for root, dirs, files in os.walk(cwd, topdown=True):
		if '.svn' in root:
			continue
		
		dircount += 1
		print root
		
		flcount = 0
		for f in files:
			parts = os.path.splitext(f)
			if parts[1] in extensions and not pattern.search(parts[0]):
				filecount += 1
				filepath = os.path.join(root, f)
				fin = open(filepath, 'r')
				
				lcount = 0
				for line in fin:
					lcount += 1
					flcount += 1
					linecount += 1
				
				fin.close()
				print '\t%s %d' % (f, lcount)
		print '\t%d' % flcount
	
	print '%d files in %d dirs' % (filecount, dircount)
	print '%d lines total' % linecount
